package cn.xmkeshe.front.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import cn.xmkeshe.dao.ICustomerDao;
import cn.xmkeshe.front.ICustomerServiceFront;
import cn.xmkeshe.front.IMailServiceFront;
import cn.xmkeshe.model.Customer;
import com.sun.corba.se.impl.presentation.rmi.IDLTypesUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ICustomerServiceFrontImpl implements ICustomerServiceFront {

  @Resource
  private ICustomerDao iCustomerDao;
  @Resource
  private IMailServiceFront iMailServiceFront;

  @Override
  @Transactional(propagation = Propagation.REQUIRED)
  public boolean add(Customer customer) {
    if(iCustomerDao.findById(customer.getCuid())==null){
      return iCustomerDao.doCreate(customer);
    }
    return false;
  }

  @Override
  @Transactional(readOnly = true)
  public Customer login(Customer customer) {

    Customer customer1 = iCustomerDao.findById(customer.getCuid());
    if (customer1 == null){
      return null;
    }else {
      if (customer1.getPassword().equals(customer.getPassword())){
        return customer1;
      }
    }
    return null;
  }

  @Override
  public boolean updateByPassword(String cuid, String password) {
    return iCustomerDao.doUpdateByPassword(cuid,password);
  }


  @Override
  @Transactional(readOnly = true)
  public Customer selectOneByEmail(String email) {
    return iCustomerDao.selectOneByEmail(email);
  }

  //  邮箱登录
  @Override
  @Transactional(readOnly = true)
  public Map<String, Object> loginAccount(Customer customer) {

    Map<String, Object> resultMap = new HashMap<>();
//    根据邮箱查询用户
    List<Customer> customers = iCustomerDao.selectByEmail(customer.getEmail());

//    查询不到结果，返回：该账户不存在或未激活
    if(customers == null ||customers.isEmpty()){
      resultMap.put("code",400);
      resultMap.put("message","该用户不存在或未激活");
      return resultMap;
    }
//    查询到多个用户，账户异常，请联系管理员
    if(customers.size()>1){
      resultMap.put("code",400);
      resultMap.put("message","账户异常，请联系管理员");
      return resultMap;
    }

//    查询到一个用户，进行密码比对
    Customer customer1 = customers.get(0);
    String password = SecureUtil.md5("{[" + customer.getPassword() + "]}");
//    密码不一致，返回密码错误
    if(!customer1.getPassword().equals(password)){
      resultMap.put("code",400);
      resultMap.put("message","用户名或者密码错误");
      return resultMap;
    }
    resultMap.put("code",200);
    resultMap.put("message","登录成功");
    return resultMap;
  }


//  激活码激活
  @Override
  @Transactional
  public Map<String, Object> activationAccount(String confirmCode) {
    Map<String, Object> resultMap = new HashMap<>();

//    根据确认码查询用户
    Customer customer = iCustomerDao.selectByConfirmCode(confirmCode);

//    判断激活时间是否超时
    boolean after = LocalDateTime.now().isAfter(customer.getActivationTime());
    if(after){
      resultMap.put("code",400);
      resultMap.put("message","连接已失效");
      return resultMap;
    }
//    修改状态码
    int result = iCustomerDao.updateByConfirmCode(confirmCode);
    if (result>0){
      resultMap.put("code",200);
      resultMap.put("message","激活成功");
    }else {
      resultMap.put("code",400);
      resultMap.put("message","激活成功");
    }

    return resultMap;
  }

  //  邮箱注册
  @Override
  public Map<String, Object> createAccount(Customer customer) {

//    确认码
    String confirmCode = IdUtil.getSnowflake(1,1).nextIdStr();
    String password = SecureUtil.md5("{[" + customer.getPassword() + "]}");
    LocalDateTime ldt = LocalDateTime.now().plusDays(1);

    customer.setPassword(password);
    customer.setConfirmCode(confirmCode);
    customer.setActivationTime(ldt);
    customer.setIsValid((byte)0);
    customer.setCredate(new Timestamp(new Date().getTime())); // 当前日期
    customer.setPhoto("user.png");

    Customer customer1 = iCustomerDao.selectOneByEmail(customer.getEmail());
    Map<String,Object> resultMap = new HashMap<>();
    if (customer1 !=null){
      resultMap.put("code","400");
      resultMap.put("message","该邮箱已被注册");
    }else {
      int result = iCustomerDao.insertCustomer(customer);
      if(result>0){
        String activationUrl = "http://localhost:8080/activation?confirmCode="+confirmCode;
        iMailServiceFront.sendMailActivationAccount(activationUrl,customer.getEmail());
        resultMap.put("code",200);
        resultMap.put("message","注册成功");
      }else{
        resultMap.put("code",400);
        resultMap.put("message","注册失败");
      }
    }
    return resultMap;
  }

}
